package ute.mvc.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ute.mvc.dao.AssignmentDAO;
import ute.mvc.dao.Assignment_StudentDAO;
import ute.mvc.dao.ClassDAO;
import ute.mvc.dao.SubmittedAssignmentDAO;
import ute.mvc.dao.UserDAO;
import ute.mvc.dto.EditAssignmentDTO;
import ute.mvc.dto.StudentAssignmentDTO;
import ute.mvc.model.Assignment;
import ute.mvc.model.Assignment_Student;
import ute.mvc.model.MyClass;
import ute.mvc.model.OpenCourse_Subject;
import ute.mvc.model.SubmittedAssignment;

@SuppressWarnings({ "unused", "unused" })
@Service("assignmentService")
public class AssignmentServiceImpl implements AssignmentService {

	@Autowired
	AssignmentDAO assignmentDAO;
	@Autowired
	ClassDAO classDAO;
	@Autowired
	UserDAO userDAO;
	@Autowired
	Assignment_StudentDAO assignment_StudentDAO;
	@Autowired
	SubmittedAssignmentDAO submittedAssignmentDAO;

	@Transactional
	public void addAssignment(Assignment assignmnent) {
		assignmentDAO.addAssignment(assignmnent);
	}

	@Transactional
	public void updateAssignment(Assignment assignmnent) {
		assignmentDAO.updateAssignment(assignmnent);
	}

	@Transactional
	public void deleteAssignment(Assignment assignmnent) {
		assignmentDAO.deleteAssignment(assignmnent);
	}

	public String getMaxIdInOpenCourseSubject(OpenCourse_Subject subject) {
		return assignmentDAO.getMaxIdInOpenCourseSubject(subject);
	}

	public Assignment getAssignmentById(String assignmentId) {
		return assignmentDAO.getAssignmentById(assignmentId);
	}

	public void updateClasses(String assignmentId, String classes) {
		String[] listClass = classes.substring(1).split(";");
		List<MyClass> lstAssignClass = new ArrayList<MyClass>();
		for (int i = 0; i < listClass.length; i++) {
			lstAssignClass.add(classDAO.getClassById(listClass[i]));
		}
		Assignment assignment = assignmentDAO.getAssignmentById(assignmentId);
		assignment.setListClass(lstAssignClass);
		assignmentDAO.updateAssignment(assignment);
	}

	public EditAssignmentDTO getEditDTOByAssignmentId(String assignmentId) {
		EditAssignmentDTO editDTO = new EditAssignmentDTO();
		Assignment assignment = assignmentDAO.getAssignmentById(assignmentId);

		editDTO.setAssignmentId(assignment.getAssignmentId());
		editDTO.setAssignmentName(assignment.getAssignmentName());
		editDTO.setDescription(assignment.getDescription());
		editDTO.setEndDate(assignment.getEndDate());
		editDTO.setMaxPoint(assignment.getMaxPoint());
		editDTO.setStartDay(assignment.getStartDay());
		editDTO.setTimeSubmitAllow(assignment.getTimeSubmitAllow());

		if (assignment.getFile() != null)
			editDTO.setFileName(assignment.getFile().getFileName());

		return editDTO;
	}

	public List<Assignment> getAssignmentsForStudent(MyClass mClass,
			OpenCourse_Subject subject) {
		List<Assignment> result = new ArrayList<Assignment>();

		Set<Assignment> assignments = subject.getListAssignment();
		List<MyClass> classes;

		for (Assignment a : assignments) {
			classes = (List<MyClass>) a.getListClass();
			for (MyClass c : classes) {
				if (c.getClassId().equals(mClass.getClassId()))
					result.add(a);
			}
		}

		return result;
	}

	public StudentAssignmentDTO getStudentAssignmentDTO(String assignmentId,
			int studentId) {
		StudentAssignmentDTO studentAssignmentDTO = new StudentAssignmentDTO();
		studentAssignmentDTO.setAllowSubmitt(false);

		Assignment assignment = assignmentDAO.getAssignmentById(assignmentId);

		Assignment_Student as = null;
		List<Assignment_Student> listAS = (List<Assignment_Student>) assignment
				.getListAssignment_Student();
		for (Assignment_Student a : listAS) {
			if (a.getStudent().getUserId() == studentId)
				as = a;
		}

		// if as is null, make new assignment_student
		if (as == null) {
			System.out.println("tao moi assignment_student");
			as = new Assignment_Student();
			as.setAssignment(assignmentDAO.getAssignmentById(assignmentId));
			as.setStudent(userDAO.getUserById(studentId));
			as.setAssignment_StudentId(assignmentId + "_" + studentId);
			assignment_StudentDAO.addAssignment_Student(as);
		}

		SubmittedAssignment submittedAssignment = as.getSubmittedAssignment();

		if (submittedAssignment != null) {
			studentAssignmentDTO.setExisted(true);
			studentAssignmentDTO.setDescription(submittedAssignment
					.getDescription());
			studentAssignmentDTO.setSubmittedTime(submittedAssignment.getSubmittedTime());
			studentAssignmentDTO.setPoint(submittedAssignment.getPoint());
			if (submittedAssignment.getFile() != null) {
				studentAssignmentDTO.setmFile(submittedAssignment.getFile());
			}
		}

		Date now = new Date();
		if ((now.after(assignment.getStartDay()) 
				&& now.before(assignment.getEndDate()))
				&& ((submittedAssignment == null) || ( submittedAssignment.getSubmittedTime() < assignment
						.getTimeSubmitAllow()))) {
			studentAssignmentDTO.setAllowSubmitt(true);
		}

		return studentAssignmentDTO;
	}

}
